<?php
/* 
 * Copyright (c) 2018-2021   All rights reserved.
 * 
 * 创建时间：2021-08-22 13:36
 *
 * 项目：rm  -  $  - H5.php
 *
 * 作者：liwei 
 */

//文档：https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_3_1.shtml
//文档2：https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=15_4
//SDK：https://github.com/wechatpay-apiv3/wechatpay-php

namespace modules\levpays\sdk\wechatpay\h5;

use Lev;
use modules\levpays\helpers\levpaysSetHelper;
use modules\levpays\sdk\wechatpay\InstancePay;
use modules\levpays\table\tradesHelper;

!defined('INLEV') && exit('Access Denied LEV');

class H5 extends InstancePay
{

    public static function tradePre() {
        return 'H5-'.levpaysSetHelper::tradePre();
    }

    public static function PlaceOrder($tradeNo, $desc, $money) {
        $instance = static::init();
        if (!$instance) return '';
        $pre = static::tradePre();
        try {
            $resp = $instance
                ->v3->pay->transactions->h5
                ->post(['json' => [
                    'mchid'        => static::$config['mchid'],
                    'out_trade_no' => InstancePay::getOutTradeNo($tradeNo, $pre),
                    'appid'        => static::$config['appid'],
                    'description'  => $desc, //'Image形象店-深圳腾大-QQ公仔',
                    'notify_url'   => InstancePay::notifyUrl(),
                    'amount'       => [
                        'total'    => $money * 100,//单位：分
                        'currency' => 'CNY'
                    ],
                    'scene_info'   => [
                        'payer_client_ip' => Lev::$app['ip'],
                        'h5_info'  => [
                            'type' => 'Wap',
                        ],
                    ],
                ]]);

            //echo $resp->getStatusCode(), PHP_EOL;
            //echo $resp->getBody(), PHP_EOL;
            $res = json_decode($resp->getBody(), true);//print_r($res);
        } catch (\Exception $e) {
            // 进行错误处理
            //echo $e->getMessage(), PHP_EOL;
            if ($e instanceof \GuzzleHttp\Exception\RequestException && $e->hasResponse()) {
                $r = $e->getResponse();
                //echo $r->getStatusCode() . ' ' . $r->getReasonPhrase(), PHP_EOL;
                //echo $r->getBody(), PHP_EOL, PHP_EOL, PHP_EOL;
                $res = json_decode($r->getBody(), true);//print_r($res);
            }
            //echo $e->getTraceAsString(), PHP_EOL;
        }
        //Controllerv::redirect($res['h5_url'].'&redirect_url='.urlencode('https://levme.com'));
        if (empty($res['h5_url'])) {
            Lev::setNotices($res['code'].'：'.$res['message']);
            InstancePay::paySuccess($res, $tradeNo, $pre);
            return '';
        }
        tradesHelper::setTradePaysource($tradeNo, $pre);
        return $res['h5_url'];
    }

    public static function checkTradeNo($tradeNo)
    {
        $tradeNo = parent::getOutTradeNo($tradeNo, static::tradePre());
        return parent::checkTradeNo($tradeNo); // TODO: Change the autogenerated stub
    }
}